from tests.rypark_background.get_cookie import *
from common.config import *
from common.Utils import Utils
import requests
from datetime import date


class Coupon:
    # 卡券起始日期设定为当前日期
    date1 = date.today()

    # 下架卡券
    def coupon_out(self, env, id):
        base_url = getconf(env, "rypark_url")
        url = base_url + "/easypark-backend/coupon/offShelves"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "cookie": shop_cookies(env)}
        return requests.get(url, headers=headers, params={"id": id}).json()

    # 卡券新建
    def create_coupon(self, env, couponName, couponType, couponHour, couponFee=''):
        base_url = getconf(env, "rypark_url")
        url = base_url + "/easypark-backend/coupon/add"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "cookie": shop_cookies(env)}
        path = data_path + "coupon_create.json"

        dict = json.loads(Utils.parse(path, {"couponFee": couponFee, "couponHour": couponHour,
                                             "couponName": couponName, "couponTimeEnd": self.date1,
                                             "couponTimeStart": self.date1, "couponType": couponType,
                                             "sendDateEnd": self.date1, "sendDateStart": self.date1,
                                             "shopUseLimit": 1, "validDateEnd": self.date1,
                                             "validDateStart": self.date1}))
        return requests.post(url, headers=headers, json=dict).json()

    # 编辑卡券
    def update_coupon(self, env, couponId, couponName, couponType, couponHour, couponFee=""):
        base_url = getconf(env, "rypark_url")
        url = base_url + "/easypark-backend/coupon/update"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "cookie": shop_cookies(env)}
        path = data_path + "coupon_update.json"
        dict = json.loads(Utils.parse(path, {"couponId": couponId, "couponFee": couponFee,
                                             "couponHour": couponHour, "couponName": couponName,
                                             "couponTimeEnd": self.date1, "couponTimeStart": self.date1,
                                             "couponType": couponType, "sendDateEnd": self.date1,
                                             "sendDateStart": self.date1, "validDateEnd": self.date1,
                                             "validDateStart": self.date1}))
        return requests.post(url, headers=headers, json=dict).json()

    # 卡券详情
    def detail_coupon(self, env, id):
        base_url = getconf(env, "rypark_url")
        url = base_url + "/easypark-backend/coupon/detail"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "cookie": shop_cookies(env)}
        return requests.get(url, headers=headers, params={"id": id}).json()

    # 商户后台卡券列表
    def shop_coupon_list(self, env, couponStatus, couponTypes, couponName=None):
        base_url = getconf(env, "rypark_url")
        url = base_url + "/easypark-backend/coupon/bsList"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "cookie": shop_cookies(env)}
        dict = {"couponName": couponName,
                "couponStatus": couponStatus,
                "couponTypes": couponTypes}
        return requests.post(url, headers=headers, json=dict).json()

    # 审核卡券
    # verifyResult：true通过 false不通过
    def check_coupon(self, env, couponId, verifyResult, verifyErrorReason=None):
        base_url = getconf(env, "rypark_url")
        url = base_url + "/easypark-backend/coupon/check"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "cookie": shop_cookies(env)}
        dict = {"couponId": couponId,
                "verifyErrorReason": verifyErrorReason,
                "verifyResult": verifyResult}
        return requests.post(url, headers=headers, json=dict).json()

    # 查询下属停车场
    def relative_parks(self, env):
        base_url = getconf(env, "rypark_url")
        url = base_url + "/easypark-backend/park/relativeParks"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "cookie": shop_cookies(env)}
        return requests.get(url, headers=headers).json()

    # 查询不同卡券的已建数量
    def count_check_coupon(self, env):
        base_url = getconf(env, "rypark_url")
        url = base_url + "/easypark-backend/coupon/countCoupon"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "cookie": shop_cookies(env)}
        return requests.get(url, headers=headers).json()

    # 统计不同状态的卡券数量
    def count_coupon(self, env):
        base_url = getconf(env, "rypark_url")
        url = base_url + "/easypark-backend/coupon/bsStatusCount"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "cookie": shop_cookies(env)}
        dict = {}
        return requests.post(url, headers=headers, json=dict).json()

    # 运营后台卡券列表
    def internal_coupon_list(self, env, couponStatus, couponTypes, couponName=None, parkName=None):
        base_url = getconf(env, "rypark_url")
        url = base_url + "/easypark-backend/coupon/innerList"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "cookie": internal_cookies(env)}
        dict = {"couponName": couponName,
                "parkName": parkName,
                "couponStatus": couponStatus,
                "couponTypes": couponTypes}
        return requests.post(url, headers=headers, json=dict).json()

    # 商户后台配置第三方卡券
    def out_coupon(self, env,couponId="",outCouponId=""):
        base_url = getconf(env, "rypark_url")
        url = base_url + "/easypark-backend/coupon/fitOutCoupon"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "cookie": shop_cookies(env)}
        dict = {"couponId": couponId,
                "outCouponId": outCouponId}
        return requests.post(url, headers=headers, json=dict).json()

    # 商户后台删除第三方卡券配置
    def delete_out_coupon(self, env,couponId=""):
        base_url = getconf(env, "rypark_url")
        url = base_url + "/easypark-backend/coupon/delFitOutCoupon"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "cookie": shop_cookies(env)}
        dict = {"couponId": couponId}
        return requests.post(url, headers=headers, json=dict).json()

    # =====================商户后台卡券交易信息=====================
    # 商户后台 查询卡券交易
    def shop_coupon_pay(self, env, couponName=None, couponNo=None, parkName=None, shopName=None,
                        status=None, useTimeStart=None, useTimeEnd=None):
        base_url = getconf(env, "rypark_url")
        url = base_url + "/easypark-backend/couponRecord/selectCouponRecordByPageBs"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "cookie": shop_cookies(env)}
        dict = {"couponName": couponName,
                "couponNo": couponNo,
                "parkName": parkName,
                "shopName": shopName,
                "status": status,
                "useTimeStart": useTimeStart,
                "useTimeEnd": useTimeEnd,
                "currentPage": 1,
                "pageSize": 10}
        return requests.post(url, headers=headers, json=dict).json()

    # 商户后台 查询卡券交易详情
    def shop_coupon_pay_detail(self, env, id):
        base_url = getconf(env, "rypark_url")
        url = base_url + "/easypark-backend/couponRecord/selectCouponRecordDetailBs"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "cookie": shop_cookies(env)}
        dict = {"id": id}
        return requests.post(url, headers=headers, json=dict).json()

    # 商户后台 导出卡券交易
    def shop_coupon_pay_export(self, env, couponName=None, couponNo=None, parkName=None, shopName=None,
                               status=None, useTimeStart=None, useTimeEnd=None):
        base_url = getconf(env, "rypark_url")
        url = base_url + "/easypark-backend/couponRecord/exportCouponRecordByPageBs"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "cookie": shop_cookies(env)}
        dict = {"couponName": couponName,
                "couponNo": couponNo,
                "parkName": parkName,
                "shopName": shopName,
                "status": status,
                "useTimeStart": useTimeStart,
                "useTimeEnd": useTimeEnd}
        return requests.post(url, headers=headers, json=dict).json()

    # =====================大运营后台卡券交易信息=====================
    # 大运营后台 查询卡券交易
    def internal_coupon_pay(self, env, couponName=None, couponNo=None, parkName=None, shopName=None,
                            status=None, useTimeStart=None, useTimeEnd=None):
        base_url = getconf(env, "rypark_url")
        url = base_url + "/easypark-backend/couponRecord/selectCouponRecordByPage"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "cookie": internal_cookies(env)}
        dict = {"couponName": couponName,
                "couponNo": couponNo,
                "parkName": parkName,
                "shopName": shopName,
                "status": status,
                "useTimeStart": useTimeStart,
                "useTimeEnd": useTimeEnd,
                "currentPage": 1,
                "pageSize": 10}
        return requests.post(url, headers=headers, json=dict).json()

    # 大运营后台 查询卡券交易详情
    def internal_coupon_pay_detail(self, env, id):
        base_url = getconf(env, "rypark_url")
        url = base_url + "/easypark-backend/couponRecord/selectCouponRecordDetail"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "cookie": internal_cookies(env)}
        dict = {"id": id}
        return requests.post(url, headers=headers, json=dict).json()

    # 大运营后台 导出卡券交易
    def internal_coupon_pay_export(self, env, couponName=None, couponNo=None, parkName=None, shopName=None,
                                   status=None, useTimeStart=None, useTimeEnd=None):
        base_url = getconf(env, "rypark_url")
        url = base_url + "/easypark-backend/couponRecord/exportCouponRecordByPage"
        headers = {"content-type": "application/json;charset=UTF-8",
                   "cookie": internal_cookies(env)}
        dict = {"couponName": couponName,
                "couponNo": couponNo,
                "parkName": parkName,
                "shopName": shopName,
                "status": status,
                "useTimeStart": useTimeStart,
                "useTimeEnd": useTimeEnd}
        return requests.post(url, headers=headers, json=dict).json()
